#
#   hkm.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   Copyright
#   Author: Salvatore S. Gionfriddo
#   Contributors: 
#   Created: 2007.07.16
#   Last Modified: 2007.07.17
#

from hud import widget, text_box
import pygame


class HotKeyMenuSystem(widget):
    def __init__(self, screen, pos, top_level = None, view_inventory = None, use_inventory = None):
        widget.__init__(self, screen, pos)
        self.top_level = top_level
        self.use_inventory = use_inventory
        self.view_inventory = view_inventory
        self.colors = [(200,200,200),(190,200,190), (200, 190, 190), (190, 190, 200)]
        if not top_level:
            self.initDefault()
        self.top_level.position = pos
        self.reset()
        
    def initDefault(self):
        hkmenu = HotKeyMenu(self.screen, (0, 0))
        
        sm = HotKeyMenu(self.screen, (0,0))
        sm.addItem("Drugs", 49, (lambda: self.use_inventory(0)), count=(lambda: self.view_inventory(0)))
        sm.addItem("Pizza", 50, (lambda: self.use_inventory(1)), count=(lambda: self.view_inventory(1)))
        sm.addItem("Indian", 51, (lambda: self.use_inventory(2)), count=(lambda: self.view_inventory(2)))
        sm.addItem("Tacos", 52, (lambda: self.use_inventory(3)), count=(lambda: self.view_inventory(3)))
        sm.addItem("Soul",53, (lambda: self.use_inventory(4)), count=(lambda: self.view_inventory(4)))
        sm.addItem("Fast Soul", 54, (lambda: self.use_inventory(5)), count=(lambda: self.view_inventory(5)))
        sm.addItem("Fat Soul", 55, (lambda: self.use_inventory(6)), count=(lambda: self.view_inventory(6)))
        sm.addItem("Strong Soul", 56, (lambda: self.use_inventory(7)), count=(lambda: self.view_inventory(7)))
        
        hkmenu.addItem("Inventory", 49, submenu=sm)
        
        self.top_level = hkmenu
        
    def dummyfunction(self):
        print 'dummy function fired'
        
    def reset(self):
        self.top_level.clearSelection()
        self.menu_stack = [self.top_level]
        self.current_menu = self.top_level
        self.function = None
        self.on = False
    def draw(self):
        for menu in self.menu_stack:
            menu.draw()
    def execute(self):
        if self.function:
            self.function()
        self.reset()
    def undo(self):
        if self.function:
            self.function = None
        elif self.current_menu.parent:
            self.menu_stack = self.menu_stack[:len(self.menu_stack)-1]
            self.current_menu = self.current_menu.parent
        self.current_menu.clearSelection()
    def keyPress(self, key):
        self.current_menu.clearSelection()
        self.function = None
        for item in self.current_menu.items:
            if item.key == key:
                if item.function:
                    self.function = item.function
                    self.execute()
                elif item.submenu:
                    self.current_menu = item.submenu
                    self.current_menu.clearSelection()
                    self.menu_stack.append(self.current_menu)
                    self.current_menu.setBackColor(self.colors[len(self.menu_stack)-1])
                    self.current_menu.reRender = True
                item.selected = True
                break

class HotKeyMenu(text_box):
    def __init__(self, screen, pos):
        self.items = []
        text_box.__init__(self,screen, pos, [])
        self.parent = None
        self.setAlpha(180)
    def render(self):
        self.strings = []
        for item in self.items:
            string = item.text
            if item.submenu:
                string += " >>"
            elif item.function and item.count != None:
                string += " (" + str(item.count()) + ")"
            if item.selected:
                string = "* - " + string
            else:
                string = pygame.key.name(item.key) + " - " + string
            if not (item.function and item.count != None and item.count() == 0):
                self.strings.append(string)
        text_box.render(self)
        for item in self.items:
            if item.submenu:
                (x,y) = self.position
                x += self.rendered_rect.width
                item.submenu.setPosition((x-5,y))
    def addItem(self, text, key, function = None, submenu = None, count = None):
        self.reRender = True
        item = HotKeyMenuItem(text, key, function, submenu, count)
        if submenu:
            submenu.parent = self
            (x,y) = self.position
            submenu.setPosition((x+120,y))
        self.items.append(item)
    def clearItems(self):
        self.items = []
    def clearSelection(self):
        self.reRender = True
        for item in self.items:
            item.selected = False

class HotKeyMenuItem:
    def __init__(self, text, key, function, submenu, count):
        self.text = text
        self.count = count
        self.key = key
        self.function = function
        self.submenu = submenu
        self.selected = False
    def setCount(self, count):
        self.count = count
